import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

Window {
    width: 600
    height: 600
    visible: true
    title: "Date 使用示例"

    ColumnLayout {
        anchors.fill: parent
        anchors.margins: 20
        spacing: 20

        // 示例1：显示当前日期的不同格式
        GroupBox {
            title: "示例1：日期格式化"
            Layout.fillWidth: true

            ColumnLayout {
                Text {
                    property date currentDate: new Date()
                    text: "当前日期（长格式）: " + currentDate.toLocaleDateString(Qt.locale(), Locale.LongFormat)
                }
                Text {
                    property date currentDate: new Date()
                    text: "当前日期（短格式）: " + currentDate.toLocaleDateString(Qt.locale(), Locale.ShortFormat)
                }
                Text {
                    property date currentDate: new Date()
                    text: "当前时间（长格式）: " + currentDate.toLocaleTimeString(Qt.locale(), Locale.LongFormat)
                }
            }
        }

        // 示例2：不同地区的日期显示
        GroupBox {
            title: "示例2：不同地区的日期显示"
            Layout.fillWidth: true

            ColumnLayout {
                Text {
                    property date currentDate: new Date()
                    text: "中文日期: " + currentDate.toLocaleString(Qt.locale("zh_CN"))
                }
                Text {
                    property date currentDate: new Date()
                    text: "英文日期: " + currentDate.toLocaleString(Qt.locale("en_US"))
                }
                Text {
                    property date currentDate: new Date()
                    text: "德文日期: " + currentDate.toLocaleString(Qt.locale("de_DE"))
                }
            }
        }

        // 示例3：日期解析
        GroupBox {
            title: "示例3：日期解析"
            Layout.fillWidth: true

            ColumnLayout {
                Text {
                    property string dateStr: "2025-04-25 14:30:00"
                    property var parsedDate: Date.fromLocaleString(Qt.locale(), dateStr, "yyyy-MM-dd hh:mm:ss")
                    text: "解析日期字符串 '" + dateStr + "'\n解析结果: " + parsedDate.toLocaleString(Qt.locale())
                }
            }
        }

        // 示例4：自定义格式
        GroupBox {
            title: "示例4：自定义格式"
            Layout.fillWidth: true

            ColumnLayout {
                Text {
                    property date currentDate: new Date()
                    text: "自定义格式1: " + currentDate.toLocaleString(Qt.locale(), "yyyy年MM月dd日 hh时mm分ss秒")
                }
                Text {
                    property date currentDate: new Date()
                    text: "自定义格式2: " + currentDate.toLocaleString(Qt.locale(), "ddd yyyy-MM-dd")
                }
            }
        }

        //示例5：日期与时间戳转换
        GroupBox {
            title: "示例5：日期与时间戳转换"
            Layout.fillWidth: true

            ColumnLayout {
                Text {
                    property date currentDate: new Date()
                    property var timestamp: currentDate.getTime()
                    text: "当前日期: " + currentDate.toLocaleString(Qt.locale()) +
                          "\n时间戳(毫秒): " + timestamp +
                          "\n时间戳(秒): " + Math.floor(timestamp / 1000)
                }

                Text {
                    property var timestamp: 1745590669000
                    property date dateFromTimestamp: new Date(timestamp)
                    text: "时间戳: " + timestamp +
                          "\n转换为日期: " + dateFromTimestamp.toLocaleString(Qt.locale())
                }
            }
        }
    }
} 
